﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class TStatusController : Controller
    {
        //
        // GET: /TStatus/

        public ActionResult Index()
        {
            return View();
        }

		/// <summary>
		/// get all statuses/by campaign
		/// </summary>
		/// <returns></returns>
		public ActionResult GetTStatus ()
		{
			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["IdCampaign"], out idCampaign);
			TStatusContainer container = TStatusModels.GetTStatusArray(idCampaign);
			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// get status info by id
		/// </summary>
		/// <returns></returns>
		public ActionResult GetTStatusById ()
		{
			decimal idTStatus = 0;
			Decimal.TryParse(Request.Params["iRec"], out idTStatus);

			TStatusContainer container = TStatusModels.GetTStatusArrayById(idTStatus);

			return Json(container, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// add/modify status
		/// </summary>
		/// <returns></returns>
		public ActionResult EditTStatus ()
		{
			decimal idTStatus = 0;
			Decimal.TryParse(Request.Params["ts_id"], out idTStatus);

			decimal idCampaign = 0;
			Decimal.TryParse(Request.Params["IdCampaign"], out idCampaign);

			TStatusModels tStatus = new TStatusModels();
			tStatus.IdCampaign = idCampaign;
			tStatus.IdTStatus = idTStatus;
			tStatus.TStatusName = Request.Params["TStatusName"];

			string messageBack = null;

			try
			{
				if (idTStatus == 0)
				{
					TStatusModels.AddTStatus(tStatus);
					messageBack = "Statusul a fost adaugat cu succes!";
				}
				else
				{
					if (idTStatus != 0)
					{
						TStatusModels.EditTStatus(tStatus);
						messageBack = "Statusul a fost modificat cu succes!";
					}
				}
				return Json(new { success = true, message = messageBack });
			}
			catch (Exception ex)
			{
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume!";
				}
				return Json(new { success = true, description = messageBack });
			}
		}
    }
}
